//
//  HJShareSendMessageView.m
//  KaiChat
//
//  Created by lx on 2024/3/8.
//

#import "HJShareSendMessageView.h"

@interface HJShareSendMessageView ()<HJShareSendMessageInputBarDelegate>

@property (nonatomic, strong) UIView *lineView;


@end

@implementation HJShareSendMessageView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        

        _responseKeyboard = YES;
        
        _lineView = [UIView addToView:self];
        _lineView.backgroundColor = UIColor.tj_separatorColor;
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.offset(0);
            make.height.offset(lineHeight);
        }];
        
        UILabel *tipLabel = [UILabel lableWithSuperView:self font:UIFontMediumMake(14) color:UIColor.tj_333333Color title:@"Share a message" textAlignment:NSTextAlignmentLeft];
        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
            make.left.offset(leftRightMargin);
            make.height.offset(40);
        }];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
        
        _inputBar = [[HJShareSendMessageInputBar alloc] initWithFrame:CGRectMake(0, 40, self.frame.size.width, 50)];
        _inputBar.delegate = self;
        [self addSubview:_inputBar];
    }
    return self;
}

- (void)keyboardWillHide:(NSNotification *)notification {
    if (!_responseKeyboard) {
        return;
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(sendMessageView:didChangeHeight:)]){
        [_delegate sendMessageView:self didChangeHeight:CGRectGetMaxY(self.inputBar.frame) + TabbarSafeBottomMargin];
    }
}

- (void)keyboardWillChangeFrame:(NSNotification *)notification {
    if (!_responseKeyboard) {
        return;
    }
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    if (_delegate && [_delegate respondsToSelector:@selector(sendMessageView:didChangeHeight:)]){
        [_delegate sendMessageView:self didChangeHeight:keyboardFrame.size.height + CGRectGetMaxY(self.inputBar.frame)];
    }
}
 

- (void)inputBar:(HJShareSendMessageInputBar *)textView didChangeInputHeight:(CGFloat)offset {
    if (_delegate && [_delegate respondsToSelector:@selector(sendMessageView:didChangeHeight:)]){
        [_delegate sendMessageView:self didChangeHeight:self.frame.size.height + offset];
    }
}

- (void)inputBar:(HJShareSendMessageInputBar *)textView didSendText:(NSString *)text {
    if(_delegate && [_delegate respondsToSelector:@selector(sendMessageView:didSendTextMessage:)]){
        [_delegate sendMessageView:self didSendTextMessage:text];
    }
    
}

- (void)inputBar:(HJShareSendMessageInputBar *)textView textViewDidBeginEditing:(NSString *)text {
    _responseKeyboard = YES;
}

- (void)inputBarDidTouchKeyboard:(HJShareSendMessageInputBar *)textView {
    
    
}

- (void)inputBarDidDeleteBackward:(HJShareSendMessageInputBar *)textView {
    
    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)reset {
    [_inputBar.inputTextView resignFirstResponder];
    if (_delegate && [_delegate respondsToSelector:@selector(sendMessageView:didChangeHeight:)]){
        [_delegate sendMessageView:self didChangeHeight:CGRectGetMaxY(self.inputBar.frame) + TabbarSafeBottomMargin];
    }
}

@end
